<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends Access_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('users_model');
		$this->load->model('crud_model');
	}

	function index()
	{
		$page["_contents"] = "administrator/user/v_user";
		$this->template->load("administrator/template/main_template",$page);
	}

	function load_list()
	{
		$arr['data'] = array();
		$query = $this->users_model->getAll();
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			array_push($data,$row->username);
			array_push($data,$row->fullname);
			array_push($data,$row->email);
			array_push($data,$row->phone);
			array_push($data,$row->status);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}
		
		echo json_encode($arr);
	}

	function add()
	{
		$data["id"] = "";
		$data["username"] = $this->input->post("username");
		$data["password"] = md5("admin123");
		$data["fullname"] = $this->input->post("fullname");
		$data["email"] = $this->input->post("email");
		$data["phone"] = $this->input->post("phone");
		$data["status"] = $this->input->post("status");

		$this->crud_model->save('users',$data);
		return true;
		
	}
	
	function edit()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->users_model->get_by_id($id)->row();
		
		$arr = array(array('id' => $id, 
						   'username' => $row->username,
						   'fullname' => $row->fullname,
						   'email' => $row->email,
						   'phone' => $row->phone,
						   'status' => $row->status
						   )
					);
		echo json_encode($arr );
		
	}
	
	function update()
	{
		$id = $this->input->post("id");;
		$data["username"] = $this->input->post("username");
		$data["fullname"] = $this->input->post("fullname");
		$data["email"] = $this->input->post("email");
		$data["phone"] = $this->input->post("phone");
		$data["status"] = $this->input->post("status");
		
		$this->crud_model->update('users', $id, $data, 'id');
		return true;
	}
	
	function delete()
	{
		$id = $this->input->post('id');
		
		$this->crud_model->delete('users', $id, 'id');
		return true;
	}

}

/* End of file user.php */
/* Location: ./application/controllers/administrator/user.php */